package org.dong.servlet;

import org.dong.Annotation.MyRequestMapping;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value = "/api/*",loadOnStartup = 1)
public class DispatchServlet extends HttpServlet {

    private Map<String,Method> hashMap = new HashMap<>();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    controlInit();
    //获取请求
         String requestURI = req.getRequestURI();

         Method method = hashMap.get(requestURI);
         if(method != null){
             Class<?> declaringClass = method.getDeclaringClass();

             try {

                 Object o = declaringClass.newInstance();
                Object[] args = new Object[]{req,resp};
                 System.out.println(Arrays.toString(args));
                 method.invoke(o,args);


             } catch (Exception e) {
                 e.printStackTrace();
             }


         }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        controlInit();
        //获取请求
        String requestURI = req.getRequestURI();

        Method method = hashMap.get(requestURI);

        if(method != null){
            Class<?> declaringClass = method.getDeclaringClass();

            try {

                Object o = declaringClass.newInstance();

                Object[] args = new Object[]{req,resp};

                method.invoke(o,args);


            } catch (Exception e) {
                e.printStackTrace();
            }


        }

    }
    public  void controlInit(){
        String rootPath = this.getClass().getResource("/").getPath();
        rootPath += "org/dong/control";
        //找出这个路径底下所有的文件
        File file = new File(rootPath);
        File[] files = file.listFiles();
        for (File f : files) {
            //获取文件名 .class
            String fileName = f.getName();
            //去掉后缀名.class
             String replaceName = fileName.replace(".class", "");

            try {

                //拿到control包里的所有类

                 Class<?> Clazz = Class.forName("org.dong.control." + replaceName);

                //获取里面所有的方法
                 Method[] methods = Clazz.getMethods();
                for (Method method : methods) {
                    if(method.isAnnotationPresent(MyRequestMapping.class)){

                         MyRequestMapping myRequestMapping = method.getDeclaredAnnotation(MyRequestMapping.class);
                         String url = myRequestMapping.value();
                         hashMap.put(url,method);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


        }
    }
}
